//
//  ViewController.m
//  ttxt
//
//  Created by 刘杰辉 on 2017/2/28.
//  Copyright © 2017年 刘杰辉. All rights reserved.
//

#import "ViewController.h"
#import "KXYXGameSDK.framework/Headers/KXYXSDKManager.h"
#import "nextViewController.h"
#import <UserNotifications/UserNotifications.h>
#define IOS8 ([[[UIDevice currentDevice] systemVersion] doubleValue] >=8.0 ? YES : NO)
@interface ViewController ()<UIWebViewDelegate>
{
    UIWebView *webView;
}
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
//    [self login];
    // 初始化本地通知对象

    //设置20秒之后
//    [[UNUserNotificationCenter currentNotificationCenter] removeAllPendingNotificationRequests];
    if (![KXYXSDKManager getUserName]) {
        [self addNotification];
    }else{
        [self cancelLocalNotification];
    }
}


- (IBAction)login{
//    [self cancelLocalNotification];
    [[KXYXSDKManager sharedVOneSDK] login:self success:^{
        [self model:nil];
        
    } failure:^(NSString * failStr) {
        NSLog(@"%@",failStr);
    }];
//    [[KXYXSDKManager sharedVOneSDK] showAssistiveView:YES];

}
// 取消某个本地推送通知
- (void)cancelLocalNotification
{
    [[UNUserNotificationCenter currentNotificationCenter] removeAllPendingNotificationRequests];
    return;
    NSString * key = @"Bind_The_Phone";
    // 获取所有本地通知数组
    NSArray *localNotifications = [UIApplication sharedApplication].scheduledLocalNotifications;
    
    for (UILocalNotification *notification in localNotifications)
    {
        NSDictionary *userInfo = notification.userInfo;
        if (userInfo)
        {
            // 根据设置通知参数时指定的key来获取通知参数
            NSString *info = userInfo[key];
            
            // 如果找到需要取消的通知，则取消
            if (info != nil)
            {
                
                [[UNUserNotificationCenter currentNotificationCenter] removePendingNotificationRequestsWithIdentifiers:@[key]];
                [[UNUserNotificationCenter currentNotificationCenter] removeDeliveredNotificationsWithIdentifiers:@[key]];
                break;
            }
        }
    }
}
- (IBAction)model:(UIButton *)sender {
    nextViewController * _vc = [self.storyboard instantiateViewControllerWithIdentifier:@"nextViewController"];
    [self presentViewController:_vc animated:YES completion:nil];
}

- (void)addNotification{
    NSString * key = @"Bind_The_Phone";
    // 获取所有本地通知数组
    NSArray *localNotifications = [UIApplication sharedApplication].scheduledLocalNotifications;
    
    for (UILocalNotification *notification in localNotifications)
    {
        NSDictionary *userInfo = notification.userInfo;
        if (userInfo)
        {
            // 根据设置通知参数时指定的key来获取通知参数
            NSString *info = userInfo[key];
            
            // 如果找到需要取消的通知，则取消
            if (info != nil)
            {
                return;
            }
        }
    }
    
    NSDate *date = [NSDate dateWithTimeIntervalSinceNow:10];
    //一个本地推送
    UILocalNotification *noti = [[[UILocalNotification alloc] init] autorelease];
    if (noti) {
        //设置推送时间
        noti.fireDate = date;//=now
        //设置时区
        noti.timeZone = [NSTimeZone defaultTimeZone];
        //设置重复间隔
        noti.repeatInterval = NSCalendarUnitDay;
        //推送声音
        noti.soundName = UILocalNotificationDefaultSoundName;
        //内容
        noti.alertBody = @"您还未绑定手机号码";
        //显示在icon上的红色圈中的数子
        noti.applicationIconBadgeNumber = 1;
        //设置userinfo 方便在之后需要撤销的时候使用
        NSDictionary *infoDic = [NSDictionary dictionaryWithObject:@"Bind_The_Phone" forKey:@"Bind_The_Phone"];
        noti.userInfo = infoDic;
        //添加推送到uiapplication
        
        UIApplication *app = [UIApplication sharedApplication];
        
        [app scheduleLocalNotification:noti];
        
    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
